1 00:00:00,560 --> 00:00:01,670 Welcome back. 2 00:00:01,670 --> 00:00:07,610 In this lecture, we're going to give the ability for our player to sprint inside of the model where 3 00:00:07,610 --> 00:00:09,320 we all grab the game assets. 4 00:00:09,320 --> 00:00:14,270 There is also a GUI for the starter GUI called the stamina GUI. 5 00:00:14,450 --> 00:00:18,380 So let me enable UI visibility so we can go ahead and take a look at this guy. 6 00:00:19,100 --> 00:00:19,910 And inside of here. 7 00:00:19,910 --> 00:00:21,140 It's a very simple guy. 8 00:00:21,140 --> 00:00:24,350 It's just one frame and another frame inside of it. 9 00:00:24,350 --> 00:00:29,870 One of them is a border and the other is a fill inside of this border. 10 00:00:30,410 --> 00:00:36,110 And all this is going to represent is a simple bar that shows the player's stamina as they continue 11 00:00:36,110 --> 00:00:36,650 sprinting. 12 00:00:36,650 --> 00:00:40,880 This bar is going to slowly go down until it reaches zero. 13 00:00:40,880 --> 00:00:45,800 Once it reaches zero, then we're going to force a cooldown on this player where they're going to have 14 00:00:45,800 --> 00:00:51,140 to wait for their stamina to replenish and come back up so they can start sprinting again. 15 00:00:51,850 --> 00:00:56,500 So let me go ahead and make these transparent again, and you'll see why we've done that in a little 16 00:00:56,500 --> 00:00:57,040 bit. 17 00:00:59,410 --> 00:01:03,040 Then I'll disable it and let me close out UI visibility. 18 00:01:03,040 --> 00:01:07,990 And now what we're going to do is we're going to create a new script inside of starter character scripts. 19 00:01:07,990 --> 00:01:09,730 It's going to be a local script. 20 00:01:09,820 --> 00:01:13,780 And the reason we're putting this inside of starter character scripts is because we want our script 21 00:01:13,780 --> 00:01:19,270 to be destroyed, and we want a new one to be copied when our players die and they respawn. 22 00:01:19,270 --> 00:01:23,620 Because all of these scripts inside of starter character scripts are going to be copied into our player's 23 00:01:23,620 --> 00:01:24,400 character. 24 00:01:24,400 --> 00:01:29,890 And then when that character dies and it gets deleted, all of these scripts along with it get deleted, 25 00:01:29,890 --> 00:01:34,150 and then a new set of scripts get copied into the new character model. 26 00:01:35,020 --> 00:01:35,650 Inside of here. 27 00:01:35,650 --> 00:01:38,470 We're going to be using that same template that we've been using before. 28 00:01:38,500 --> 00:01:41,410 We're going to get rid of this section as well as this section. 29 00:01:41,410 --> 00:01:45,880 And what we're going to need to do is we're going to need to listen for a user's input. 30 00:01:45,880 --> 00:01:50,710 Specifically, for most games, when you want to sprint, you'll typically hold down the left shift 31 00:01:50,710 --> 00:01:51,250 key. 32 00:01:51,250 --> 00:01:57,610 So the best way for us to listen for that user input, as well as listen for across multiple different 33 00:01:57,610 --> 00:02:01,540 devices would be to use the Context action service. 34 00:02:01,540 --> 00:02:04,630 So we'll go ahead and grab the Context Action service. 35 00:02:08,350 --> 00:02:14,320 And then we're also going to need the player service in order to grab the stamina guy or create a reference 36 00:02:14,320 --> 00:02:18,820 to that guy so we can go ahead and manipulate it to show how much stamina we currently have. 37 00:02:18,820 --> 00:02:20,590 So we'll get the player service as well. 38 00:02:21,570 --> 00:02:27,150 And of course, we're also going to want to use the tween service to fade our stamina guy in and out 39 00:02:27,150 --> 00:02:28,110 of the screen. 40 00:02:28,110 --> 00:02:32,220 So, for example, if we're not sprinting in, all of our stamina has been filled up, then we want 41 00:02:32,220 --> 00:02:33,840 to fade the guy out. 42 00:02:33,840 --> 00:02:37,890 And then once we start sprinting and using stamina, we want to fade the guy back in. 43 00:02:37,890 --> 00:02:40,320 So we're going to need to use the tween service for that. 44 00:02:42,560 --> 00:02:48,020 And the last but not least, we're going to want to constantly listen or update the stamina for the 45 00:02:48,020 --> 00:02:49,790 player basically every single frame. 46 00:02:49,790 --> 00:02:52,670 So the other service we're going to need is the run service. 47 00:02:55,540 --> 00:02:59,770 Now, how this is going to work is we're going to use the context action service, and we're going to 48 00:02:59,770 --> 00:03:03,100 bind an action to a specific set of inputs. 49 00:03:03,100 --> 00:03:05,110 So we'll create a new action here. 50 00:03:05,110 --> 00:03:07,030 I'm just going to call this action sprint. 51 00:03:07,510 --> 00:03:09,460 And then we can go ahead and bind our function. 52 00:03:09,460 --> 00:03:15,220 And this will get passed the action name the input state as well as our input object. 53 00:03:15,770 --> 00:03:17,000 So we'll do that. 54 00:03:17,300 --> 00:03:20,600 If we would like to create a touch button, I'm going to do true. 55 00:03:20,600 --> 00:03:23,240 So that way we can add support for mobile devices. 56 00:03:23,240 --> 00:03:29,090 And then the other input that we want to listen to is going to be the enum dot key code dot left shift. 57 00:03:30,770 --> 00:03:36,050 And now inside of this function we want to do is we want to make sure that the input state is equal 58 00:03:36,050 --> 00:03:41,630 to the enum dot user input state of begin, or the user input state of end. 59 00:03:41,630 --> 00:03:47,480 So that way we can keep track of when they start sprinting and when they release and stop sprinting. 60 00:03:47,660 --> 00:03:53,420 So we can check if either it's in the begin input state, or if the input state is equal to the enum 61 00:03:53,420 --> 00:03:55,820 dot user input state dot end. 62 00:03:58,350 --> 00:04:02,880 And what we can go ahead and do is we can create a variable to keep track of whether or not we are currently 63 00:04:02,880 --> 00:04:03,660 sprinting. 64 00:04:03,690 --> 00:04:06,660 So I'll create a variable, I'll call it shift down. 65 00:04:06,660 --> 00:04:08,730 And by default we're going to set that to false. 66 00:04:08,730 --> 00:04:12,090 But when we start sprinting we're going to set shift down equal to true. 67 00:04:12,090 --> 00:04:15,960 And then when we stop sprinting we're going to set shift down back equal to false. 68 00:04:16,480 --> 00:04:20,200 And then another thing we're going to have to keep track of is the player's stamina. 69 00:04:20,200 --> 00:04:25,210 So we're also going to want to have a variable that, uh, contains what our current stamina is. 70 00:04:25,210 --> 00:04:26,890 So I'll just call it current stamina. 71 00:04:26,890 --> 00:04:30,190 And by default we're going to set that at a value of 100. 72 00:04:30,190 --> 00:04:32,950 So we can either go to zero or up to 100. 73 00:04:33,610 --> 00:04:39,640 Now of course we don't want to set shift down equal to true if we do not have enough stamina. 74 00:04:39,640 --> 00:04:48,910 So if our current stamina is, um, less than or equal to zero, then we're just going to return because 75 00:04:48,910 --> 00:04:50,710 we don't want to let the player sprint. 76 00:04:51,500 --> 00:04:56,420 Now, another thing that we can do to make it look like our player is sprinting is that we can also 77 00:04:56,420 --> 00:04:59,660 tween the field of view of the player's camera. 78 00:04:59,660 --> 00:05:05,330 So if you've ever played a game and you sprint in a game, you can see that your camera kind of zooms 79 00:05:05,330 --> 00:05:08,030 out and then when you stop sprinting, it kind of zooms back in. 80 00:05:08,030 --> 00:05:12,440 That's us shifting or changing the field of view of the camera. 81 00:05:12,440 --> 00:05:17,810 So what we could do is we could create two dedicated functions, one to start sprinting and one to stop 82 00:05:17,810 --> 00:05:18,440 sprinting. 83 00:05:18,440 --> 00:05:23,600 Where the start sprinting will update the walk speed of our humanoid and then zoom out the camera a 84 00:05:23,600 --> 00:05:29,210 little bit, and then the stop function will zoom the camera back to its original field of view, and 85 00:05:29,210 --> 00:05:29,780 then reset. 86 00:05:29,780 --> 00:05:32,870 The humanoids walk speed back to its default. 87 00:05:33,610 --> 00:05:39,820 So what we're going to go ahead and do is create several constants to keep track of a bunch of information. 88 00:05:39,820 --> 00:05:43,750 For example, what speed would we like to set the character to when they sprint? 89 00:05:43,750 --> 00:05:49,510 We could call it sprint speed, and we could set it to a value of like 22 studs per second. 90 00:05:49,780 --> 00:05:54,910 Another constant we could keep track of is what is the regular or default speed for our humanoid. 91 00:05:54,910 --> 00:05:57,010 So we could call this a regular speed. 92 00:05:57,010 --> 00:05:59,260 And that means we're going to have to grab our humanoid. 93 00:05:59,260 --> 00:06:03,190 So up here I'll create a variable one to refer to our character. 94 00:06:03,190 --> 00:06:08,800 And that's going to just be equal to script dot parent, because this script is going to be a child 95 00:06:08,950 --> 00:06:10,450 of our player's character. 96 00:06:10,450 --> 00:06:14,800 And actually let's go ahead and rename this script to our sprint handler. 97 00:06:16,710 --> 00:06:20,910 And then we can also go ahead and grab the humanoid, which is going to be equal to the character, 98 00:06:20,910 --> 00:06:23,250 and we'll use wait for child just in case. 99 00:06:23,250 --> 00:06:25,410 And then we can go ahead and grab our humanoid. 100 00:06:25,530 --> 00:06:29,310 And then we'll also make a quick reference to the GUI as well. 101 00:06:29,310 --> 00:06:31,740 So that's going to be inside of our player. 102 00:06:31,740 --> 00:06:34,170 So let's make a quick reference to the player as well. 103 00:06:34,170 --> 00:06:35,910 So players dot local player. 104 00:06:36,120 --> 00:06:38,850 And then GUI is going to be equal to our player. 105 00:06:39,300 --> 00:06:41,910 And that's going to be in the player GUI folder. 106 00:06:41,910 --> 00:06:46,110 And we want to go ahead and wait for the stamina GUI. 107 00:06:46,740 --> 00:06:50,520 And then we can go ahead and set regular speed equal to our humanoids walk speed. 108 00:06:52,400 --> 00:06:56,960 And then some other constants we want to go ahead and define is what's our maximum stamina that we can 109 00:06:56,960 --> 00:06:57,200 have. 110 00:06:57,200 --> 00:06:59,180 So we can call this max stamina. 111 00:06:59,180 --> 00:07:01,520 And we could just set it to 100. 112 00:07:02,210 --> 00:07:08,360 And then we also want to define how fast we want to replenish or drain away our stamina. 113 00:07:08,360 --> 00:07:12,410 So I could create some constants like stamina reduce. 114 00:07:12,710 --> 00:07:16,280 And we'll just set it to some arbitrary value like 32. 115 00:07:16,490 --> 00:07:18,500 And then we could do another one. 116 00:07:18,500 --> 00:07:20,690 We'll call it stamina increase. 117 00:07:21,280 --> 00:07:23,500 And we'll set it equal to like 16. 118 00:07:23,500 --> 00:07:29,140 So basically what I want these values to represent is that every single second our stamina is going 119 00:07:29,140 --> 00:07:30,820 to be reduced by 32. 120 00:07:30,820 --> 00:07:34,900 And then every single second our stamina is going to be increased by 16. 121 00:07:34,900 --> 00:07:40,810 So our stamina as it stands currently should last us about a little over three seconds. 122 00:07:40,810 --> 00:07:44,380 Of course, you can change these values to whatever you like, but I'm just going to set them to these 123 00:07:44,380 --> 00:07:45,100 for now. 124 00:07:45,640 --> 00:07:50,320 And then we also want to define what our field of view should be when we're sprinting, and what our 125 00:07:50,320 --> 00:07:52,060 default field of view is. 126 00:07:52,060 --> 00:07:54,790 So we'll call this sprint field of view. 127 00:07:55,530 --> 00:08:00,270 And if we go ahead and look in the workspace and look at our camera, the default field of view is 70. 128 00:08:00,270 --> 00:08:03,810 So we could set it to something a little bit higher like 85. 129 00:08:04,050 --> 00:08:06,510 And then we can also store the regular field of view. 130 00:08:06,510 --> 00:08:08,520 And that means we're going to have to grab the camera. 131 00:08:08,520 --> 00:08:10,530 So I'll make a variable for the camera real quick. 132 00:08:10,530 --> 00:08:11,640 We'll just call this camera. 133 00:08:11,640 --> 00:08:13,980 And that's equal to workspace dot current camera. 134 00:08:15,060 --> 00:08:20,760 And the regular field of view is going to be equal to our camera dot field of view. 135 00:08:21,650 --> 00:08:28,100 And then we can also define how long we would like to wait before we start replenishing stamina if we've 136 00:08:28,100 --> 00:08:29,150 drained through all of it. 137 00:08:29,150 --> 00:08:31,910 So we could call this our sprint cooldown. 138 00:08:31,910 --> 00:08:38,060 Or you could call it our stamina cooldown and we could set it to something like five seconds. 139 00:08:38,060 --> 00:08:42,590 So the player is going to have to wait five seconds if they let their stamina drain all the way down 140 00:08:42,590 --> 00:08:43,310 to zero. 141 00:08:44,160 --> 00:08:47,130 But now what we could go ahead and do is we could create two functions. 142 00:08:47,130 --> 00:08:54,510 One function we could call start sprint, and then the other one we could call reset speed. 143 00:08:55,230 --> 00:09:00,750 So this one will have a start sprinting and will zoom the field of view of the camera out, and then 144 00:09:00,750 --> 00:09:05,040 we'll reset our walk speed back to default and zoom the camera back in. 145 00:09:05,250 --> 00:09:09,930 So inside of our start sprint function, what we want to do is we want to check if the camera's field 146 00:09:09,930 --> 00:09:14,220 of view is not equal to the sprint field of view. 147 00:09:14,310 --> 00:09:19,590 If it isn't, then that means we're going to want to tween the field of view of the camera to match 148 00:09:19,590 --> 00:09:20,580 our sprint field of view. 149 00:09:20,580 --> 00:09:23,850 So we use the tween service we'll create on our camera. 150 00:09:26,450 --> 00:09:34,190 We could do a tween info dot new of like 0.8 seconds, and then we want to change the field of view 151 00:09:34,190 --> 00:09:39,320 of our camera to be equal to the sprint field of view, and then we can go ahead and play this tween. 152 00:09:42,260 --> 00:09:48,470 And then we also want to set the humanoids walk speed equal to our sprint speed. 153 00:09:50,450 --> 00:09:53,840 And then for the reset speed function, we'll just copy this. 154 00:09:53,900 --> 00:10:00,140 But instead this time we want to check if the field of view of the camera is not equal to the original 155 00:10:00,140 --> 00:10:02,240 field of view or defaults. 156 00:10:02,240 --> 00:10:02,990 What do I call it? 157 00:10:02,990 --> 00:10:03,530 Regular. 158 00:10:03,560 --> 00:10:03,860 Yeah. 159 00:10:03,860 --> 00:10:05,060 Regular field of view. 160 00:10:05,740 --> 00:10:10,780 And if it isn't, then we want to set it to the regular field of view and then reset the walk speed 161 00:10:10,780 --> 00:10:14,890 to the default or regular walk speed. 162 00:10:14,890 --> 00:10:15,970 So speed. 163 00:10:15,970 --> 00:10:16,720 Regular speed. 164 00:10:16,720 --> 00:10:17,470 There we go. 165 00:10:18,350 --> 00:10:24,230 So any time we press down shift and we have enough stamina, then we're going to start our sprint. 166 00:10:24,230 --> 00:10:32,660 Otherwise when we let go of the shift key, then we're going to stop our sprint or reset the speed of 167 00:10:32,660 --> 00:10:33,650 our character. 168 00:10:34,010 --> 00:10:38,600 Now the thing is, as we hold down shift, we're going to want to drain the player's stamina. 169 00:10:38,600 --> 00:10:42,320 And then when it hits zero, we're going to want to reset the speed of the player. 170 00:10:42,320 --> 00:10:49,130 So we're going to have to listen to the run service heartbeat event and connect a function to this. 171 00:10:50,340 --> 00:10:54,630 And this is where we're going to update every single frame to drain the stamina. 172 00:10:54,630 --> 00:10:57,330 If the shift down variable is equal to true. 173 00:10:57,360 --> 00:11:02,070 Now, another important thing to note is that this function gets passed something called a delta time. 174 00:11:02,220 --> 00:11:03,420 Delta time. 175 00:11:03,420 --> 00:11:08,040 And this variable represents the amount of time between frames. 176 00:11:08,040 --> 00:11:12,570 So how long did it take at the start of one frame to the start of the next frame. 177 00:11:12,720 --> 00:11:18,090 And if your game is running at 60 frames per second, then the time between those two frames is going 178 00:11:18,090 --> 00:11:20,430 to be about 0.016 seconds. 179 00:11:20,430 --> 00:11:27,000 However, this delta time is important, and that's because if we're draining the stamina of our player 180 00:11:27,000 --> 00:11:33,300 based on their frame rate or based on how fast this heartbeat updates, if a player is playing the game 181 00:11:33,300 --> 00:11:40,050 at a lower or higher frame rate, and we do not take into account this delta time value, then the stamina 182 00:11:40,050 --> 00:11:44,100 is either going to reduce faster or slower for some players. 183 00:11:44,100 --> 00:11:49,740 But if we want to keep it consistent between all players in our game, regardless of their frame rate, 184 00:11:49,740 --> 00:11:52,710 then we need to use delta time in our calculations. 185 00:11:53,100 --> 00:11:58,320 So in this heartbeat event, what we're going to do is we're going to check if we're holding down shift, 186 00:11:58,620 --> 00:12:03,360 and we're also going to want to check and make sure that our current stamina is greater than the value 187 00:12:03,360 --> 00:12:04,260 of zero. 188 00:12:04,260 --> 00:12:08,670 And we also want to make sure that our player is actually moving. 189 00:12:09,170 --> 00:12:14,060 And to demonstrate this, what I'm going to do real quick is I'm going to denote this as a humanoid, 190 00:12:14,060 --> 00:12:19,070 because the linter or basically studio doesn't know the type of this humanoid. 191 00:12:19,070 --> 00:12:23,570 So when I type it out, as you can see, it doesn't really auto fill any important information. 192 00:12:23,570 --> 00:12:26,420 So I'm just going to use some type annotation real quick. 193 00:12:26,420 --> 00:12:32,600 You don't need to worry about what this is, but it's going to help, uh, studio recognize that this 194 00:12:32,600 --> 00:12:34,820 variable is storing a humanoid. 195 00:12:34,820 --> 00:12:40,310 So when I go and type it out now, I get auto filled with all the stuff that belongs to a humanoid. 196 00:12:41,200 --> 00:12:47,650 Because when I go down here, I want to make sure that the humanoid dot move direction property, it 197 00:12:47,650 --> 00:12:51,010 says, describes the direction that the humanoid is walking in. 198 00:12:51,010 --> 00:12:53,200 And we're going to get the magnitude of that vector. 199 00:12:53,200 --> 00:12:58,150 We want to make sure that's greater than zero, which confirms to us that the humanoid or this player 200 00:12:58,150 --> 00:12:59,800 is actually walking. 201 00:13:00,800 --> 00:13:07,310 So if we're holding down shift, we have enough stamina and we're actually moving, then that means 202 00:13:07,310 --> 00:13:09,710 we want to drain the stamina of our player. 203 00:13:09,710 --> 00:13:16,340 So we want to update the value of current stamina to be minus equal to the stamina reduce. 204 00:13:16,340 --> 00:13:20,210 And then we want to multiply this value by our delta time. 205 00:13:20,210 --> 00:13:22,580 So we're going to multiply it by delta time. 206 00:13:22,580 --> 00:13:25,010 So that way we keep it consistent. 207 00:13:25,010 --> 00:13:30,080 Or we're reducing the stamina consistently across multiple different frame rates. 208 00:13:30,680 --> 00:13:36,920 Otherwise if our stamina for example reaches zero then we want to force a cooldown. 209 00:13:37,130 --> 00:13:40,040 So what we could do is we could create another boolean. 210 00:13:40,040 --> 00:13:43,160 And I'm going to call this sprinting cooldown. 211 00:13:43,160 --> 00:13:44,960 And by default we're going to set it to false. 212 00:13:44,960 --> 00:13:52,400 But what we're going to check here is we're going to check if we are not in a sprinting cooldown. 213 00:13:52,490 --> 00:13:56,690 If we're not in a sprinting cooldown, then we need to check if we don't have any stamina. 214 00:13:56,690 --> 00:14:02,900 So if our current stamina is less than or equal to zero and we're not in a sprinting cooldown, well 215 00:14:02,900 --> 00:14:05,510 then we need to force a sprinting cooldown. 216 00:14:05,510 --> 00:14:09,830 So what we're going to do is we're going to set sprinting cooldown equal to true. 217 00:14:09,860 --> 00:14:12,740 We're going to set current stamina equal to zero. 218 00:14:13,460 --> 00:14:19,070 And now what we want to do is we want to reset the speed of our player because we're out of stamina. 219 00:14:19,070 --> 00:14:20,930 So we'll do reset speed. 220 00:14:20,930 --> 00:14:28,460 And then we basically want to like delay a function from executing, which is going to allow us to start 221 00:14:28,460 --> 00:14:29,960 replenishing stamina. 222 00:14:30,200 --> 00:14:35,720 Because another thing we want to check is that if we are not holding down shift. 223 00:14:37,260 --> 00:14:42,930 So if our current stamina is less than our maximum stamina. 224 00:14:44,340 --> 00:14:48,990 And we want to make sure that we can actually replenish stamina. 225 00:14:49,350 --> 00:14:56,520 So if current stamina is less than our maximum stamina, um, we need to create a variable to keep track 226 00:14:56,520 --> 00:14:59,130 of whether or not we can actually replenish stamina. 227 00:14:59,130 --> 00:15:02,850 So I'm going to create a variable called can replenish stamina. 228 00:15:02,850 --> 00:15:05,250 And by default we're going to set that to true. 229 00:15:06,260 --> 00:15:13,700 And then what we're going to do is when we reach a sprinting cool down, we want to set can replenish 230 00:15:13,700 --> 00:15:16,490 stamina equal to false. 231 00:15:16,490 --> 00:15:19,790 And then we're also going to want to set shift down equal to false. 232 00:15:19,790 --> 00:15:22,520 We're basically resetting everything because we're out of stamina. 233 00:15:23,090 --> 00:15:26,030 And then we want to delay a function from executing. 234 00:15:26,030 --> 00:15:28,880 And that delay is going to be equal to our sprint cooldown. 235 00:15:30,200 --> 00:15:36,260 And once that sprint cooldown is up, then we can set can replenish stamina back to true to let our 236 00:15:36,260 --> 00:15:40,460 function connected to our heartbeat know that hey, we can start replenishing stamina. 237 00:15:41,030 --> 00:15:46,520 So in this if statement right here, when our current stamina is less than our max stamina, then we 238 00:15:46,520 --> 00:15:49,550 want to verify that we can actually replenish stamina. 239 00:15:49,550 --> 00:15:55,430 So if we cannot replenish stamina, then we're just going to return because we are on our cooldown, 240 00:15:55,430 --> 00:15:57,440 so we don't want to replenish any stamina. 241 00:15:57,950 --> 00:16:04,430 Otherwise, we want to make sure that we're not holding down shift or our player's character is not 242 00:16:04,430 --> 00:16:05,390 moving. 243 00:16:05,630 --> 00:16:13,250 So if we are not holding down the shift key or our humanoids move direction property, get the magnitude 244 00:16:13,250 --> 00:16:13,490 of that. 245 00:16:13,490 --> 00:16:20,540 If that's less than or equal to zero, then we can go ahead and replenish stamina right here because 246 00:16:20,540 --> 00:16:24,440 our player could hold down shift, but their character could not be moving. 247 00:16:24,440 --> 00:16:27,710 So we want to still make sure that we're replenishing stamina for them. 248 00:16:27,710 --> 00:16:31,820 So this time we're going to set current stamina plus equal to. 249 00:16:31,820 --> 00:16:34,340 And then we're going to get our stamina increase. 250 00:16:34,340 --> 00:16:37,880 And then also multiply this by our delta time. 251 00:16:38,420 --> 00:16:43,790 And then inside of here we're also going to want to make sure that because we can replenish stamina 252 00:16:43,790 --> 00:16:51,140 just in case we're going to set sprinting cooldown back to false because we don't set a sprinting cooldown 253 00:16:51,140 --> 00:16:52,550 to false up here. 254 00:16:52,550 --> 00:16:58,910 But we do it down here because when it tells us that we can replenish stamina, then it's going to hop 255 00:16:58,910 --> 00:16:59,270 in here. 256 00:16:59,270 --> 00:17:04,790 It's going to make sure we can replenish stamina, and then it'll set sprinting cooldown equal to false 257 00:17:04,790 --> 00:17:05,540 for us. 258 00:17:06,420 --> 00:17:11,610 And now, since here we are tracking the stamina of our player. 259 00:17:11,880 --> 00:17:17,820 What we're going to want to do is, while our current stamina is less than our max stamina, we want 260 00:17:17,820 --> 00:17:24,060 to update our stamina bar so we could create a dedicated function to update the stamina bar to set the 261 00:17:24,060 --> 00:17:29,190 bar equal to whatever percentage stamina we have left. 262 00:17:29,700 --> 00:17:31,710 So let's go ahead and create a function. 263 00:17:32,070 --> 00:17:35,760 And I'm going to call this function Update Stamina bar. 264 00:17:35,760 --> 00:17:39,360 And we're going to call this function inside of this if statement. 265 00:17:39,360 --> 00:17:44,760 So we're only going to update our stamina bar when we've noticed that our current stamina is less than 266 00:17:44,760 --> 00:17:46,320 our maximum stamina. 267 00:17:46,830 --> 00:17:53,640 So inside of this function, what we're going to want to do is since by default, our stamina guy is 268 00:17:53,640 --> 00:17:59,820 going to be hidden and not on the screen, we want to check if our stamina guy is hidden, and if it 269 00:17:59,820 --> 00:18:03,150 is, we want to unhide it and fade it onto the screen. 270 00:18:03,150 --> 00:18:07,950 Because we have been using stamina and we want to display that to the player. 271 00:18:08,190 --> 00:18:13,830 So if our guy is not enabled, then we want to make sure our guy is enabled. 272 00:18:14,680 --> 00:18:22,060 And then we want to go ahead and tween the fill as well as the outline for the fill. 273 00:18:22,060 --> 00:18:25,210 We want to update their transparency properties to be opaque. 274 00:18:25,360 --> 00:18:29,140 So again we're going to use the tween service. 275 00:18:29,140 --> 00:18:31,000 So tween service create. 276 00:18:31,620 --> 00:18:34,050 One's going to be on the outline. 277 00:18:35,370 --> 00:18:41,220 And then another one is going to be on our fill frame. 278 00:18:42,750 --> 00:18:49,950 And then for the tween info dot new, we could do something like fade it in for like 0.3 seconds. 279 00:18:51,270 --> 00:18:54,450 And we want to make sure we update the transparency to be opaque. 280 00:18:54,450 --> 00:18:55,860 So equal to zero. 281 00:18:57,210 --> 00:19:00,810 And then we can copy this and do the same thing for our fill frame. 282 00:19:00,810 --> 00:19:06,270 But for the fill frame, it doesn't have a transparency property, but it does have a background transparency 283 00:19:06,270 --> 00:19:07,050 property. 284 00:19:08,010 --> 00:19:14,250 And then we can go ahead and play these two tweens to fade the guy onto our screen. 285 00:19:15,350 --> 00:19:24,530 And then now we also want to, um, update that bar or the fill frame once we've displayed the guy on 286 00:19:24,530 --> 00:19:25,130 the screen. 287 00:19:25,130 --> 00:19:30,470 So what we could do is refer to our guy, get the border, and then get the fill frame. 288 00:19:30,470 --> 00:19:36,410 And we want to update its size, and its size is going to be equal to a new Utum two. 289 00:19:36,410 --> 00:19:38,420 And we're going to do from scale. 290 00:19:39,050 --> 00:19:45,920 And then for the x axis, that's just going to be equal to one because our stamina bar is a vertical. 291 00:19:45,920 --> 00:19:48,230 So we want to update it on the y axis. 292 00:19:48,230 --> 00:19:51,800 So the y axis is going to be equal to our current stamina. 293 00:19:51,800 --> 00:19:55,970 And we're going to divide it by our max stamina to get our percentage. 294 00:19:55,970 --> 00:20:01,280 So if our current stamina is 100 and our max stamina is 100, then that's a value of one. 295 00:20:01,280 --> 00:20:02,810 So it will be completely filled. 296 00:20:02,810 --> 00:20:05,150 But if our current stamina is like 50. 297 00:20:05,840 --> 00:20:07,310 Then it's going to be half. 298 00:20:07,310 --> 00:20:09,380 So the Y scale is going to be 0.5. 299 00:20:09,380 --> 00:20:11,960 So we'll see that we only have half of our stamina. 300 00:20:13,360 --> 00:20:22,180 And then one more thing we want to go ahead and check for is we want to check for if our stamina is 301 00:20:22,180 --> 00:20:23,440 at 100. 302 00:20:23,440 --> 00:20:26,980 If it is, then we want to fade the guy back out. 303 00:20:26,980 --> 00:20:36,760 So one way for us to check it is we can check if our stamina, if our current stamina is equal to 100. 304 00:20:36,880 --> 00:20:45,130 Or another way we could check it is if our guy dot border dot fill dot size on the y scale if that size 305 00:20:45,130 --> 00:20:48,460 is equal to one. 306 00:20:49,800 --> 00:20:53,070 And then we also want to make sure that it hasn't been faded out yet. 307 00:20:53,070 --> 00:21:03,240 So we can also check if the guy dot boarder dot outline dot transparency is still equal to zero. 308 00:21:03,810 --> 00:21:07,680 And then let me go ahead and wrap this all up. 309 00:21:10,030 --> 00:21:13,690 So that way the interpreter knows how to interpret this if statement. 310 00:21:13,690 --> 00:21:22,870 So if either current stamina is equal to 100, or the Y scale on our UI is set to one, and we haven't 311 00:21:22,870 --> 00:21:29,200 faded it out yet because the transparency is still set to zero, then we need to fade out our UI. 312 00:21:29,380 --> 00:21:32,620 So I'm just going to copy what we did up here. 313 00:21:34,300 --> 00:21:37,330 But this time we want to set the transparency to one. 314 00:21:39,460 --> 00:21:42,370 And then I'm going to grab one of these tweens. 315 00:21:42,370 --> 00:21:44,200 So I'll just call this tween. 316 00:21:45,680 --> 00:21:48,290 Make, get rid of that and then we'll play this tween. 317 00:21:48,290 --> 00:21:50,690 And then we want to listen for when this tween completes. 318 00:21:50,690 --> 00:21:59,030 Because when this tween completes we want to go ahead and disable our guy. 319 00:21:59,060 --> 00:22:03,020 So we'll set guy dot enabled equal to false. 320 00:22:03,960 --> 00:22:04,560 Okay. 321 00:22:04,560 --> 00:22:09,540 We should now have everything we need set up for our sprint system. 322 00:22:09,990 --> 00:22:16,560 So when we press down the left shift key, we're going to set shift down equal to true. 323 00:22:16,560 --> 00:22:18,240 And we're going to start sprinting. 324 00:22:18,240 --> 00:22:24,030 Then it's going to check if we are currently holding down shift and we are moving and we have enough 325 00:22:24,030 --> 00:22:24,420 stamina. 326 00:22:24,420 --> 00:22:26,130 It's going to reduce our stamina. 327 00:22:26,730 --> 00:22:31,920 Once we recognize that our stamina is less than or equal to zero, we're going to update its value equal 328 00:22:31,920 --> 00:22:34,380 to zero, and we're going to force a cooldown. 329 00:22:34,380 --> 00:22:37,710 And while we're forcing this cooldown can replenish. 330 00:22:37,710 --> 00:22:43,560 Stamina is set to false, which tells this section of code right here that hey, you can't replenish 331 00:22:43,560 --> 00:22:45,180 stamina, so don't do anything. 332 00:22:45,890 --> 00:22:50,150 Otherwise, if we can replenish stamina, then we're going to make sure that we're not holding down 333 00:22:50,150 --> 00:22:54,110 shift, or we're going to make sure that our humanoid is not walking. 334 00:22:54,110 --> 00:22:59,450 If we aren't, then we're going to go ahead and restore the stamina for our player. 335 00:22:59,780 --> 00:23:03,320 And once we do that, we can go ahead and update the stamina bar. 336 00:23:03,740 --> 00:23:09,440 And when we update the stamina bar, that's either going to fade in or fade out the guy on our screen, 337 00:23:09,440 --> 00:23:15,800 depending on how much stamina we have, and we're updating the size of our fill frame to be equal to 338 00:23:15,800 --> 00:23:18,440 the percentage of how much stamina we have. 339 00:23:19,050 --> 00:23:24,810 Now, one more thing we could do is that we could listen for when our humanoid dies. 340 00:23:25,630 --> 00:23:30,790 And when our humanoid dies, we want to go ahead and stop listening to this input right here, because 341 00:23:30,790 --> 00:23:32,470 we can't sprint when we're dead. 342 00:23:32,470 --> 00:23:39,490 So we could use the context action service and unbind the action of sprint when our humanoid dies. 343 00:23:39,640 --> 00:23:42,430 Other than that, I think we should be good to go. 344 00:23:42,460 --> 00:23:45,070 This was a lot of code to write without testing anything. 345 00:23:45,070 --> 00:23:50,140 So let's go ahead and actually playtest our game and see if we are able to sprint. 346 00:23:50,140 --> 00:23:51,520 So we'll hit play here. 347 00:23:51,520 --> 00:23:52,810 Spawn in the map. 348 00:23:53,230 --> 00:23:55,630 Here's my player if I hold down shift. 349 00:23:56,700 --> 00:24:00,360 As you can see, the screen zooms out as I start walking. 350 00:24:00,360 --> 00:24:00,930 Boom! 351 00:24:00,930 --> 00:24:01,710 We're running. 352 00:24:01,710 --> 00:24:02,970 We're reducing stamina. 353 00:24:02,970 --> 00:24:07,110 And then when I let go, as you can see, the stamina is replenishing itself. 354 00:24:09,360 --> 00:24:13,770 And it got to the full amount, but unfortunately it is not fading out. 355 00:24:13,770 --> 00:24:17,880 So let's go ahead and figure out why that is the case. 356 00:24:18,580 --> 00:24:23,530 But otherwise it does appear that our stamina system is indeed working. 357 00:24:23,530 --> 00:24:27,730 When we sprint, the camera zooms out and then when we stop, it zooms back in. 358 00:24:27,730 --> 00:24:31,420 Let me go ahead and hit stamina zero. 359 00:24:31,420 --> 00:24:32,230 There we go. 360 00:24:32,230 --> 00:24:35,350 Now we're in a force cooldown and I can no longer sprint. 361 00:24:35,890 --> 00:24:39,160 And now we're able to replenish, and then I can sprint again. 362 00:24:40,090 --> 00:24:46,120 So it looks like the only thing we need to troubleshoot is why our GUI is not disappearing when we hit 363 00:24:46,120 --> 00:24:47,560 max stamina. 364 00:24:48,640 --> 00:24:55,570 Okay, I think one reason why it might not exactly be working is because our current stamina could go 365 00:24:55,570 --> 00:24:58,930 above the value of 100 when we're increasing it here. 366 00:24:58,930 --> 00:25:04,690 So what we could do to account for that is instead of set this from equal to to be greater than or equal 367 00:25:04,690 --> 00:25:04,840 to. 368 00:25:04,870 --> 00:25:10,720 So if the current stamina is greater than or equal to 100, then we can go ahead and fade out the guy. 369 00:25:10,930 --> 00:25:14,650 So let's go ahead and test that out and see how that would work. 370 00:25:18,770 --> 00:25:20,780 So if I go on sprint and then stop. 371 00:25:21,660 --> 00:25:22,290 There we go. 372 00:25:22,290 --> 00:25:22,830 Perfect. 373 00:25:22,830 --> 00:25:26,580 Our guy fades out and it's no longer visible on the screen. 374 00:25:26,580 --> 00:25:30,210 And then when I start sprinting again, it reappears on the screen. 375 00:25:30,210 --> 00:25:36,150 And then when the bar goes back up and our current stamina perfect as you can see, it fades back out 376 00:25:36,150 --> 00:25:36,720 again. 377 00:25:36,720 --> 00:25:37,620 Beautiful. 378 00:25:39,190 --> 00:25:41,170 That's all from me for this lecture. 379 00:25:41,170 --> 00:25:44,590 I hope you enjoyed and I'll see you in the next one.